package com.asiainfo.dao;
/*
 * 
 * 卡数据库操作类
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.asiainfo.bean.Card;
import com.asiainfo.database.JdbcTemplete;
import com.asiainfo.database.ResultSetHandler;

public class Card_Dao {
	// 添加卡
	public int add(Card card) {
		JdbcTemplete jt = new JdbcTemplete();
		List<Card> cards = findUsingCardsByVipId(card.getVip_id());
		//判断添加卡的人有没有正在使用的卡
		if(cards.size()==0){
			card.setState(2);
		}
		String sql = "insert into card(card_type,card_date,card_num,vip_id,card_effective,card_state) values (?,?,?,?,?,?)";
		return jt.update(sql, card.getType(), card.getDate(), card.getNum(), card.getVip_id(), card.getEffective(),
				card.getState());
	}

	// 根据id查找卡
	public Card findCardById(int id) {
		JdbcTemplete jt = new JdbcTemplete();
		String sql = "select card_type,card_date,card_num,vip_id,card_effective,card_state,card_id from card where card_id=?";
		Card card = (Card) jt.quary(sql, new ResultSetHandler() {
			public Object doHandler(ResultSet rs) throws SQLException {
				Card card = new Card();
				if (rs.next()) {
					card.setType(rs.getInt(1));
					card.setId(rs.getInt(7));
					card.setDate(rs.getInt(2));
					card.setNum(rs.getInt(3));
					card.setVip_id(rs.getInt(4));
					card.setEffective(rs.getInt(5));
					card.setState(rs.getInt(6));
				}
				return card;
			}
		}, id);
		return card;
	}

	// 更新卡信息
	public int update(Card card) {
		JdbcTemplete jt = new JdbcTemplete();
		String sql = "update card set card_type=?,card_date=?,card_num=?,vip_id=?,card_effective=?,card_state=? where card_id=?";
		return jt.update(sql, card.getType(), card.getDate(), card.getNum(), card.getVip_id(), card.getEffective(),
				card.getState(), card.getId());
	}

	// 查询某用户所有未激活的卡
	public List<Card> findCardsByVipId(int vip_id) {
		JdbcTemplete jt = new JdbcTemplete();
		String sql = "select card_type,card_date,card_num,vip_id,card_effective,card_state,card_id from card where vip_id=? and card_state = 1";
		@SuppressWarnings("unchecked")
		List<Card> cards = (List<Card>) jt.quary(sql, new ResultSetHandler() {
			public Object doHandler(ResultSet rs) throws SQLException {
				Card card;
				List<Card> cards = new LinkedList<Card>();
				while (rs.next()) {
					card = new Card();
					card.setType(rs.getInt(1));
					card.setId(rs.getInt(7));
					card.setDate(rs.getInt(2));
					card.setNum(rs.getInt(3));
					card.setVip_id(rs.getInt(4));
					card.setEffective(rs.getInt(5));
					card.setState(rs.getInt(6));
					cards.add(card);
				}
				return cards;
			}
		},vip_id);
		return cards;
	}

	// 查询某用户所有已激活的卡
	public List<Card> findUsingCardsByVipId(int vip_id) {
		JdbcTemplete jt = new JdbcTemplete();
		String sql = "select card_type,card_date,card_num,vip_id,card_effective,card_state,card_id from card where vip_id=? and card_state = 2";
		@SuppressWarnings("unchecked")
		List<Card> cards = (List<Card>) jt.quary(sql, new ResultSetHandler() {
			public Object doHandler(ResultSet rs) throws SQLException {
				Card card;
				List<Card> cards = new LinkedList<Card>();
				while (rs.next()) {
					card = new Card();
					card.setType(rs.getInt(1));
					card.setId(rs.getInt(7));
					card.setDate(rs.getInt(2));
					card.setNum(rs.getInt(3));
					card.setVip_id(rs.getInt(4));
					card.setEffective(rs.getInt(5));
					card.setState(rs.getInt(6));
					cards.add(card);
				}
				return cards;
			}
		},vip_id);
		return cards;
	}
}
